<?php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Laravel\Lumen\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule $schedule
     * @return void
     */
    protected function schedule( Schedule $schedule )
    {
        // 每隔10分钟更新express数据表
        $schedule->call( function () {

            $expressLists = \App\Models\Express::where( 'state', '!=', 3 )->get();

            foreach ( $expressLists as $item ) {
                $queryResult = (new \Xu42\ExpressTracking\ExpressTracking( $item['nu'] ))->latestStatus();
                if ( (strtotime( $queryResult['data'][0]['time'] )) > strtotime( $item['time'] ) ) {
                    // 更新数据库表信息
                    \App\Models\Express::whereId( $item['id'] )->update( [
                        'state'   => $queryResult['state'],
                        'context' => $queryResult['data'][0]['context'],
                        'time'    => $queryResult['data'][0]['time']
                    ] );
                    // 推送通知
                    dispatch( new \App\Jobs\SendExpressNoticeJob( $item ) );
                }
            }
        } )->everyFiveMinutes();
    }
}
